package com.diana.serviceImpl;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.diana.common.PageUtils;
import com.diana.common.StatusEnums;
import com.diana.common.Utils;
import com.diana.entity.ManagerRole;
import com.diana.entity.ManagerRoleMenu;
import com.diana.mapper.ManagerRoleMapper;
import com.diana.service.ManagerMainService;
import com.diana.service.ManagerRoleMenuService;
import com.diana.service.ManagerRoleService;
import com.diana.service.QueueMainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author anzhi
 * @since 2019-04-21
 */
@Service
public class ManagerRoleServiceImpl extends ServiceImpl<ManagerRoleMapper, ManagerRole> implements ManagerRoleService {

    @Autowired
    private QueueMainService queueMainService;
    @Autowired
    private ManagerRoleMapper managerRoleMapper;
    @Autowired
    private ManagerRoleMenuService managerRoleMenuService;
    @Autowired
    private ManagerMainService managerMainService;

    /**
     * 新增角色
     * @param map
     * @return
     */
    @Transactional
    @Override
    public String addRole(Map<String, Object> map) {
        String roleName = Utils.toString(map.get("roleName"));
        String remark  = Utils.toString(map.get("remark"));
        String permissions = Utils.toString(map.get("permissions"));
        if(Utils.isNullOrEmpty(roleName)){
            return Utils.message("0001","请输入角色名称",null);
        }
        if(Utils.isNullOrEmpty(permissions)){
            return Utils.message("0001","请选择权限",null);
        }
        ManagerRole managerRole = selectOne(Condition.create().eq("roleName", roleName).ne("status", StatusEnums.CommonStatus.删除));
        if(managerRole!=null){
            return Utils.message("0001","该角色已存在",null);
        }

        managerRole = new ManagerRole();
        String roleId = queueMainService.getQueueValue("roleId");

        managerRole.setRoleId(roleId);
        managerRole.setRoleName(roleName);
        managerRole.setRemark(remark);
        managerRole.setStatus(StatusEnums.CommonStatus.可用);

        String[] arr = permissions.split(",");

        ManagerRoleMenu managerRoleMenu = null;
        List<ManagerRoleMenu> list = new ArrayList<>();
        for(int i = 0;i<arr.length;i++){
            managerRoleMenu= new ManagerRoleMenu();
            managerRoleMenu.setMenuId(arr[i]);
            managerRoleMenu.setRoleId(roleId);
            managerRoleMenu.setStatus(StatusEnums.CommonStatus.可用);
            list.add(managerRoleMenu);
        }
        boolean batch = managerRoleMenuService.insertBatch(list);
        boolean insert = insert(managerRole);
        if(batch&&insert){
            return Utils.message("0000","操作成功",null);
        }
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        return Utils.message("0001","操作失败",null);
    }

    /**
     * 修改角色信息
     * @param map
     * @return
     */
    @Transactional
    @Override
    public String updateRole(Map<String, Object> map) {
        String roleId = Utils.toString(map.get("roleId"));
        String roleName = Utils.toString(map.get("roleName"));
        String remark  = Utils.toString(map.get("remark"));
        Integer status = Utils.toInteger(map.get("status"));
        String permissions = Utils.toString(map.get("permissions"));
        if(Utils.isNullOrEmpty(roleId)){
            return Utils.message("0001","请选择roleId",null);
        }
        if(Utils.isNullOrEmpty(roleName)){
            return Utils.message("0001","请输入roleName",null);
        }
        if(Utils.isNullOrEmpty(permissions)){
            return Utils.message("0001","请选择权限",null);
        }
        ManagerRole managerRole = selectOne(Condition.create().eq("roleName", roleName).ne("status", StatusEnums.CommonStatus.删除).ne("roleId",roleId));
        if(managerRole!=null){
            return Utils.message("0001","角色已存在",null);
        }
        //查询该角色下是否存在用户，存在则不能失效
        if(status==StatusEnums.CommonStatus.不可用){
            int count = managerMainService.selectCount(Condition.create().eq("roleId", roleId).ne("status", StatusEnums.CommonStatus.删除));
            if(count>0){
                return Utils.message("0001","角色下有用户，请先删除用户",null);
            }
        }
        //修改
        managerRole = new ManagerRole();
        managerRole.setRoleId(roleId);
        managerRole.setStatus(status);
        managerRole.setRemark(remark);
        managerRole.setRoleName(roleName);
        boolean update = updateById(managerRole);

        //删除该角色原来权限
        boolean delete = managerRoleMenuService.delete(Condition.create().eq("roleId", roleId));

        //新增权限
        String[] arr = permissions.split(",");
        ManagerRoleMenu managerRoleMenu = null;
        List<ManagerRoleMenu> list = new ArrayList<>();
        for(int i = 0;i<arr.length;i++){
            managerRoleMenu= new ManagerRoleMenu();
            managerRoleMenu.setMenuId(arr[i]);
            managerRoleMenu.setRoleId(roleId);
            managerRoleMenu.setStatus(StatusEnums.CommonStatus.可用);
            list.add(managerRoleMenu);
        }
        boolean batch = managerRoleMenuService.insertBatch(list);

        if(update&&delete&&batch){
            return Utils.message("0000","操作成功",null);
        }
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        return Utils.message("0001","操作失败",null);
    }

    /**
     * 删除角色
     * @param map
     * @return
     */
    @Override
    public String updateStatus(Map<String, Object> map) {
        String roleId = Utils.toString(map.get("roleId"));
        Integer status = Utils.toInteger(map.get("status"));
        if(Utils.isNullOrEmpty(roleId)){
            return Utils.message("0001","请选择roleId",null);
        }
        if(status==null){
            return Utils.message("0001","请选择status",null);
        }
        ManagerRole managerRole = selectOne(Condition.create().eq("roleId", roleId).ne("status", StatusEnums.CommonStatus.删除));
        if(managerRole==null){
            return Utils.message("0001","该角色不存在",null);
        }
        int count = managerMainService.selectCount(Condition.create().eq("roleId", roleId).ne("status", StatusEnums.CommonStatus.删除));
        if(count>0){
            return Utils.message("0001","角色下有用户，请先删除用户",null);
        }
        managerRole = new ManagerRole();
        managerRole.setRoleId(roleId);
        managerRole.setStatus(status);
        if(updateById(managerRole)){
            return Utils.message("0000","操作成功",null);
        }
        return Utils.message("0001","操作失败",null);
    }

    /**
     * 获取角色信息
     * @param map
     * @return
     */
    @Override
    public String getRoleInfo(Map<String,Object> map) {
        String roleId = Utils.toString(map.get("roleId"));
        Map<String,Object> Info = selectMap(Condition.create().eq("roleId", roleId).ne("status", StatusEnums.CommonStatus.删除));
        List<ManagerRoleMenu> list = managerRoleMenuService.selectList(Condition.create().eq("roleId", roleId).ne("status", StatusEnums.CommonStatus.删除));
        String permissions = "";
        String menuId = "";
        if(list.size()>0){
            for(int i = 0;i<list.size();i++){
                menuId = list.get(i).getMenuId();
                permissions = permissions+menuId+",";
            }
        }
        if(permissions!=""){
            permissions = permissions.substring(0,permissions.length() - 1);
        }
        Info.put("permissions",permissions);
        return Utils.message("0000","获取成功",Info);
    }

    /**
     * 获取角色列表
     * @param map
     * @return
     */
    @Override
    public String getRoleList(Map<String, Object> map) {
        String roleName = Utils.toString(map.get("roleName"));
        Integer status = Utils.toInteger(map.get("status"));
        Integer index = Utils.toInteger(map.get("index"));
        Integer size = Utils.toInteger(map.get("size"));

        Page<Map<String, Object>> page = PageUtils.getPage(index, size);
        String joinsql = "";
        if(!Utils.isNullOrEmpty(roleName)){
            joinsql += " and a.roleName like '%"+roleName+"%' " ;
        }
        if(status!=null){
            joinsql += " and a.status = "+status+" " ;
        }
        List<Map<String, Object>> list = managerRoleMapper.getRoleList(joinsql, page);
        page.setRecords(list);
        return Utils.message("0000","获取成功",page.getRecords(),page.getTotal());
    }
}
